<?php
namespace app\index\controller;

use app\common\model\CompanyDailyShan as CompanyDailyShanModel;

/**
 * 日行一善控制器
 * Class Companydailyshan
 * @package app\index\controller
 */
class Companydailyshan extends Base
{
    /**
     * [index  日行一善]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $modal = new CompanyDailyShanModel;
        $user = $this->user;
        try{
            $data = $modal->getOne(['comid'=>$user['comid']],'id','money,intro,donor_num');
            if(empty($data)){
                $data = config('dailyshan');
            }else{
                $data['money'] = unserialize($data['money']);
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        writelog($user['mid'], $user['comid'], '查看日行一善');
        return admin_json(1, '获取成功',$data);
    }
    /**
     * [edit  编辑]
     * @return json
     * @author [C。]
     */
    public function edit()
    {
        $modal = new CompanyDailyShanModel;
        $param = input('param.');
        $user = $this->user;
        try{
            $data = $modal->getOne(['comid'=>$user['comid']]);
            $param['money'] = serialize($param['money']);
            if(empty($data)){
                $param['comid'] = $user['comid'];
                $modal->addData($param);
            }else{
                $modal->editDataBy($param,['comid'=>$user['comid']]);
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        writelog($user['mid'], $user['comid'], '编辑日行一善');
        return admin_json(1, '编辑成功');
    }
    /**
     * [restore  还原默认]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function restore()
    {
        $user = $this->user;
        try{
            $gongdeng = config('dailyshan');
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        writelog($user['mid'], $user['comid'], '还原默认日行一善配置');
        return admin_json(1, '获取成功',$gongdeng);
    }
}